   package jdbc;

   import java.sql.Connection;
   import java.sql.PreparedStatement;
   import java.sql.ResultSet;
   import java.sql.SQLException;
   import java.util.Scanner;

   public class LoginDemo {
       public static void main(String[] args) {
           Scanner scanner = new Scanner(System.in);
           System.out.print("请输入用户名: ");
           String username = scanner.nextLine();
           System.out.print("请输入密码: ");
           String password = scanner.nextLine();

           if (login(username, password)) {
               System.out.println("登录成功");
           } else {
               System.out.println("登录失败");
           }
       }

       public static boolean login(String username, String password) {
           String sql = "SELECT * FROM user WHERE username = ? AND password = ?";
           try (Connection connection = DBUtil.getConnection();
                PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
               preparedStatement.setString(1, username);
               preparedStatement.setString(2, password);
               ResultSet resultSet = preparedStatement.executeQuery();
               return resultSet.next();
           } catch (SQLException e) {
               e.printStackTrace();
               return false;
           }
       }
   }

